import testObserver.Data;
import testObserver.Weather;
import testProxy.DynamicSubject;
import testProxy.Subject;
import testRectangle.Point;
import testRectangle.Rectangle;
import testReflect.MyClassLoader;
import testSort.TestSort;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static String getRandomRecoveryPassword(int passwordLength) {
        if (passwordLength <= 0 || passwordLength > 255) return null;
        StringBuffer password = new StringBuffer();
        Random randomNumber = new Random();
        for (int i = 0; i < passwordLength; i++) {
            password.append(randomNumber.nextInt(10));
        }
        return password.toString();
    }

    public static void main(String[] args) throws Exception {
//		testProxy();
//		testReflect();
//		testClassLoad();
//		testObserver();
//		testGetInteractionRectangleArea();
//		testSearchANumberFromAnotherNumber();
//		testQuickSort();
//		testInsertSort();
//		testGetPolygonArea();
//		TestJNI.helloworld();
//	    testConsoleInput();

//        System.out.print(getRandomRecoveryPassword(8));

//        String test = "奈";
//        byte[] utf8 = test.getBytes();
//        byte[] iso = test.getBytes("ISO-8859-1");
//
//        if (utf8.length != iso.length) {
//            System.out.println("not the same length::" + utf8.length + "::" + iso.length);
//        } else {
//            for (int i = 0; i < utf8.length; i++) {
//                if (utf8[i] != iso[i]) {
//                    System.out.println("22222");
//                    break;
//                }
//            }
//        }
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
//                new DateFormatSymbols(Locale.FRANCE));
//        sdf1.setTimeZone(TimeZone.getTimeZone("GMT"));
//        long aa = 1931200200000L;
//        System.out.println(sdf.format(new Date(aa)));
//        System.out.println(sdf1.format(new Date(aa)));
//
//        if (sdf.equals(sdf1)) {
//            System.out.println("111111111111111");
//        }
//
//        System.out.println(String.format("T%1$,03d", 0));
//
//        System.out.println("" + Integer.parseInt("-1"));
//        if (Integer.parseInt("-1") == -1) {
//            System.out.println(String.format("T%1$,03d", 0));
//        }
//
//        System.out.println("The same");

        String cn = "*.fr";
        if (cn.indexOf('.', 2) != -1) {
            System.out.println("11111");
        }
        if (cn.lastIndexOf('.') >= 0) {
            System.out.println("44444");
        }
        if (cn.startsWith("*.")) {
            System.out.println("22222");
        }

        new Thread() {
            public void run() {
                try {
                    String host = "www.baidu.com";
                    InetAddress[] addrs = InetAddress.getAllByName(host);
                    for (InetAddress add : addrs) {
                        System.out.println("IP::" + add);
                    }
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public static void testConsoleInput() {
        System.out.println("--testConsoleInput--start--\n");

        Scanner scan = new Scanner(System.in);
        System.out.println("-------> " + scan.nextInt());
        System.out.println("-------> " + scan.nextLine());
        System.out.println("-------> " + scan.nextLine());

        System.out.println("\n--testConsoleInput---end---");
    }

    public static void testGetPolygonArea() {
        System.out.println("--testGetPolygonArea--start--\n");

        Point[] points = new Point[5];
        points[0] = new Point(0, 0);
        points[1] = new Point(10, 0);
        points[2] = new Point(10, 10);
        points[3] = new Point(5, 15);
        points[4] = new Point(0, 10);
        double area = 0.0;
        for (int i = 1; i < points.length - 1; i++) {
            area += Rectangle.getTriangleArea(points[0], points[i], points[i + 1]);
        }
        System.out.println("This polygon's area is: " + area);

        System.out.println("\n--testGetPolygonArea---end---");
    }

    public static void testInsertSort() {
        System.out.println("--testInsertSort--start--\n");

        int[] array = new int[] {
                11, 8, 1, 3, 3, 5, 1, 4, 3, 5, 6
        };
        TestSort.insertSort(array);

        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();

        System.out.println("\n--testInsertSort---end---");
    }

    public static void testQuickSort() {
        System.out.println("--testQuickSort--start--\n");

        int[] array = new int[] {
                11, 8, 1, 3, 3, 5, 1, 4, 3, 5, 6
        };
        TestSort.quickSort(array, 0, array.length - 1);

        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();

        System.out.println("\n--testQuickSort---end---");
    }

    public static void testSearchANumberFromAnotherNumber() {
        System.out.println("--testSearchANumberFromAnotherNumber--start--\n");

        Integer i = new Integer(23245435);
        Integer j = new Integer(45);
        String a = i.toString();
        String b = j.toString();
        System.out.println("-------> " + a.indexOf(b));

        System.out.println("\n--testSearchANumberFromAnotherNumber---end---");
    }

    public static void testPigEatSeed() {
        System.out.println("--testPigEatSeed--start--\n");

        int allSeeds[][] = {//
                /*//////*/{
                        1, 8, 9, 3
                } //
                , {
                        2, 7, 4, 9
                } //
                , {
                        3, 6, 6, 3
                } //
                , {
                        7, 9, 4, 8
                }
        //
        };
        eatSeed(0, 0, allSeeds);

        System.out.println("\n--testPigEatSeed---end---");
    }

    private static int index;
    private static int[][] path;

    private static void addPath(int x, int y) {
        boolean isNotExist = true;
        for (int i = 0; i < index; i++) {
            if (path[i][0] == x && path[i][1] == y) {
                isNotExist = false;
            }
        }

        if (isNotExist) {
            path[index][0] = x;
            path[index][1] = y;
            index++;
        }
    }

    private static int eatSeed(int row, int col, int[][] array) {
        if (row == array.length - 1 && col == array[0].length - 1) {
            return array[row][col];
        } else if (row == array.length - 1) {
            return array[row][col] + eatSeed(row, col + 1, array);
        } else if (col == array[0].length - 1) {
            return array[row][col] + eatSeed(row + 1, col, array);
        }
        int right = array[row][col] + eatSeed(row, col + 1, array);
        int down = array[row][col] + eatSeed(row + 1, col, array);
        if (right > down) {
            addPath(row, col + 1);
        } else {
            addPath(row + 1, col);
        }
        return Math.max(right, down);
    }

    public static void testGetInteractionRectangleArea() {
        System.out.println("--testGetInteractionRectangleArea--start--\n");

        Rectangle rect1 = new Rectangle(new Point(0, 0), new Point(232, 34234));
        Rectangle rect2 = new Rectangle(new Point(0, 0), new Point(3242, 435));
        System.out.println("Area is: " + Rectangle.getIntersectionRectangleArea(rect1, rect2));

        System.out.println("\n--testGetInteractionRectangleArea---end---");
    }

    public static void testObserver() {
        System.out.println("--testObserver--start--\n");

        class Test1 extends Weather {
            public Test1(Data d) {
                super(d);
            }

            @Override
            protected void onChange() {
                System.out.println(Test1.class.getName() + " --> " + getTemperature());
            }
        }

        class Test2 extends Weather {
            public Test2(Data d) {
                super(d);
            }

            @Override
            protected void onChange() {
                System.out.println(Test2.class.getName() + " --> " + getTemperature());
            }
        }

        Data data = new Data();
        Test1 t1 = new Test1(data);
        Test2 t2 = new Test2(data);
        data.registerListener(t1);
        data.registerListener(t2);
        t1.setTemperature(3);
        t1.setTemperature(4);

        System.out.println("\n--testObserver---end---");
    }

    public static void testClassLoad() throws Exception {
        System.out.println("--testClassLoad--start--\n");

        MyClassLoader loader = new MyClassLoader();
        Class<?> c = loader.loadClass("TestClassLoad.RemoteClass");
        Object obj = c.newInstance();
        c.getMethod("aMethod").invoke(obj);

        System.out.println("\n--testClassLoad---end---");
    }

    public static void testReflect() throws Exception {
        System.out.println("--testReflect--start--\n");

        Class<?> c = Class.forName("testProxy.RealSubject");
        Object obj = c.newInstance();
        c.getMethod("test", String.class).invoke(obj, "reflect");

        System.out.println("\n--testReflect---end---");
    }

    public static void testProxy() throws Exception {
        System.out.println("--testProxy--start--\n");

        //        RealSubject obj = new RealSubject();
        //        Class<? extends RealSubject> c = obj.getClass();

        Class<?> c = Class.forName("testProxy.RealSubject");
        Object obj = c.newInstance();

        InvocationHandler ds = new DynamicSubject(obj);
        Subject subject = (Subject)Proxy
                .newProxyInstance(c.getClassLoader(), c.getInterfaces(), ds);
        String s = subject.test("Test");
        System.out.println("Printed from Main-> " + s);

        System.out.println("\n--testProxy---end---");
    }
}
